/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.dao.*;
import model.pojo.*;
import org.hibernate.Session;

/**
 *
 * @author CUCAINHO
 */
@WebServlet(name = "main", urlPatterns = {"/main"})
public class main extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private String page;

    protected void DoDanhSachDeal(HttpServletRequest request) {
        List<Deal> danhSachDeal = null;
        if (request.getParameter("id") != null) {
            String strMaDanhMuc = (String) request.getParameter("id");
            int maDanhMuc = Integer.parseInt(strMaDanhMuc);
            danhSachDeal = DealDAO.LayDanhSachDeal(maDanhMuc);

        } else {
            danhSachDeal = DealDAO.LayDanhSachDeal();

        }

        request.setAttribute("danhSachDeal", danhSachDeal);
    }

    protected void DoMain(HttpServletRequest request) {
        List<Deal> danhSachDeal = null;

        danhSachDeal = DealDAO.LayDanhSachDeal();
        Deal dealMoiNhat = danhSachDeal.get(0);
        danhSachDeal.remove(0);
        // lay danh sach hinh show
        int maDeal = dealMoiNhat.getMaDeal();
        List<Hinh> danhSachHinh = HinhDAO.DanhSachHinh(maDeal);

        request.setAttribute("danhSachHinh", danhSachHinh);

        //
        request.setAttribute("dealtop", dealMoiNhat);
        request.setAttribute("danhSachDeal", danhSachDeal);

    }

    protected void DoChiTietDeal(HttpServletRequest request) {
        String strMaDeal = null;
        if (request.getParameter("id") != null) {
            strMaDeal = (String) request.getParameter("id");
            int maDeal = Integer.parseInt(strMaDeal);
            Chitietdeal chiTietDeal = ChiTietDealDAO.LayChiTietDeal(maDeal);

            Deal deal = DealDAO.LayDeal(maDeal);
            int maCongTy = chiTietDeal.getMaCongTy();
            Congty congTy = CongTyDAO.LayCongTy(maCongTy);
            List<Hinh> danhSachHinh = HinhDAO.DanhSachHinh(maDeal);

            request.setAttribute("danhSachHinh", danhSachHinh);

            request.setAttribute("congTy", congTy);
            request.setAttribute("chiTietDeal", chiTietDeal);
            request.setAttribute("deal", deal);
            request.setAttribute("dealtop", deal);
        }


    }
     protected void DoChiTietDealSearch(HttpServletRequest request) {
        String strMaDeal = null;
        
        String search = null;
        int typeSearch ;
        
        //getParameter
        
        search = request.getParameter("txtTimKiem");
        typeSearch = Integer.parseInt(request.getParameter("cboLoai"));
        
                
        
        if (request.getParameter("id") != null) {
            strMaDeal = (String) request.getParameter("id");
            int maDeal = Integer.parseInt(strMaDeal);
            Chitietdeal chiTietDeal = ChiTietDealDAO.LayChiTietDeal(maDeal);

            Deal deal = DealDAO.LayDeal(maDeal);
            int maCongTy = chiTietDeal.getMaCongTy();
            Congty congTy = CongTyDAO.LayCongTy(maCongTy);
            List<Hinh> danhSachHinh = HinhDAO.DanhSachHinh(maDeal);

            request.setAttribute("danhSachHinh", danhSachHinh);

            request.setAttribute("congTy", congTy);
            request.setAttribute("chiTietDeal", chiTietDeal);
            request.setAttribute("deal", deal);
            request.setAttribute("dealtop", deal);
        }


    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        // Khai báo các thuộc tính
        List<Danhmuc> danhSachDanhMuc = DanhMucDao.LayDanhSachDanhMuc();
        List<Diadiem> danhSachDiaDiem = DiaDiemDAO.LayDanhSachDiem();


        // end khai báo các thuộc tính
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            HttpSession session = request.getSession();
            page = request.getParameter("page");
            if (page == null) {
                page = "Main";
            }
            String action = request.getParameter("action");

            String login = "<span class='link_top'><a href='#' class='button_login' >Login</a></span>";
            String logout = "";

            if (session.getAttribute("isLogin") != null) {
                Khachhang kh = (Khachhang) session.getAttribute("isLogin");
                logout = "<span class='link_top'>Xin chào:<b>" + kh.getHoTen() + "</b>   <a href='Logout'>Logout</a></span>";
                request.setAttribute("log", logout);

            } else {
                request.setAttribute("log", login);
            }


            String url = "";
            /// phần điều hướng các trang

            if (page == "Main") {
                DoMain(request);
                url = "users/index.jsp";
            } else if (page.equals("DanhSachDeal")) {

                DoDanhSachDeal(request);
                // set data cho danh sach deal
                // List<Deal> danhSachDeal = DealDAO.LayDanhSachDeal();
                // request.setAttribute("danhSachDeal",danhSachDeal);

                //
                url = "users/DanhSachDeal.jsp";
            } else if (page.equals("DangKy")) {
                url = "users/DangKy.jsp";
            } else if (page.equals("ChiTietDeal")) {
                DoChiTietDeal(request);
                url = "users/ChiTietDeal.jsp";
            } else if (page.equals("ChiTietDealSearch")) {
                DoChiTietDealSearch(request);
                url = "users/ChiTietDeal.jsp";
            } else {
                DoMain(request);
                url = "users/index.jsp";
            }
            // end phần điều hướng các trang


            // set thuộc tính cho view
            request.setAttribute("danhSachDanhMuc", danhSachDanhMuc);
            request.setAttribute("danhSachDiaDiem", danhSachDiaDiem);

            //end set thuộc tính cho view
            RequestDispatcher rd = request.getRequestDispatcher(url);
            rd.forward(request, response);

            //end send data
        } finally {
            out.close();
        }
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
